import React from 'react'
import { connect } from 'dva'
import { Radio } from 'antd'

/**
 * [TimeRadio 时间段单选组合]
 */
class TimeRadio extends React.Component {
  state = {
    data: this.props.data,
    value: this.props.value,
  }

  componentWillReceiveProps(nextProps) {
    if(nextProps.value!==this.props.value){
      this.setState({
        value:nextProps.value,
      })
    }
  }

  handleChange = (e) => {
    
    this.setState({
      value:e.target.value,
    })
    this.changeRouter(e.target.value)
  }

  changeRouter(val) {
    //  保存查询参数到URL
    const { pathname, query: { timeRange, ...query } } = this.props.location
    this.props.dispatch({
      type: `app/changeRouter`,
      payload: {
        pathname,
        query: {
          timeRange: val,
          ...query,
        }
      }
    })
  }
  render() {
    const { value, data } = this.state
    console.log(67701,this.props);
    
    
    return (
      <Radio.Group onChange={this.handleChange.bind(this)} value={value}>
          {
            data.map((item, index) => <Radio.Button value={item.value} key={index}>{item.name}</Radio.Button>)
          }
        </Radio.Group>
    )
  }
}
TimeRadio.defaultProps = {
  value: '0',
  data: [],
  location: {
    pathname: '/',
    query: {},
  },
}

export default connect()(TimeRadio)
